package com.elvt.server.controller;

import com.elvt.server.pojo.Admin;
import com.elvt.server.pojo.AdminLoginParam;
import com.elvt.server.service.IAdminService;
import com.elvt.server.utils.RespSult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * @Author 作者： elvt
 * @Data 时间：2022/5/22
 * @packageName 包路径： ybg_back_end
 * @Desciption 说明： 登录 退出
 */
@Api(tags = "LoginController")
@RestController
public class LoginController {
    @Autowired
    private IAdminService adminService;
    @ApiOperation(value = "登录接口之后返回token")
    @PostMapping("/login")
    public RespSult login(@RequestBody AdminLoginParam adminLoginParam, HttpServletRequest request){

            return adminService.login(adminLoginParam.getUsername(),adminLoginParam.getPassword(),adminLoginParam.getCode(),request);

    }


    @ApiOperation(value = "获取当前用户信息")
    @GetMapping("/admin/info")
    public Admin getAdminInfo(Principal principal){
        if (null==principal){
            return null;
        }
        String username = principal.getName();
      Admin admin=adminService.getAdminByUserName(username);
      admin.setPassword(null);
      admin.setRoles(adminService.getRolesByAdmin(admin.getId()));
      return admin;
    }


    @ApiOperation(value = "退出登录")
    @PostMapping("/logout")
    public RespSult logout(){

        return RespSult.success("退出成功");
    }

}
